import { approvalPieceApi } from "@/api/approval/myApproval";
import { message } from "ant-design-vue";

export default function OperationHook(state: Base.GeneraDataType) {
  const { formDataRef, formState, emit, passData, isFinance, loading } = state;
  const onSubmit = (state: number) => {
    formDataRef.value.validate().then(() => {
      const { file, remark, isCloseCase, companyProportion, bizProportion } = formState;

      const params = {
        state,
        id: passData.value.id,
        remark,
        file: file.join(",")
      };
      if (isFinance.value) {
        Object.assign(params, { isCloseCase, companyProportion, bizProportion });
        params.file = file.join(",");
      }

      loading.value = true;
      return approvalPieceApi(params)
        .then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功");
            emit("submit");
          }
        })
        .finally(() => {
          loading.value = false;
        });
    });
  };

  const resetForm = () => {
    formDataRef.value.resetFields();
    emit("update:visible", false);
  };

  const changeCompany = () => {
    formState.bizProportion = 10 - formState.companyProportion;
  };
  const changePerson = () => {
    formState.companyProportion = 10 - formState.bizProportion;
  };
  return {
    onSubmit,
    resetForm,
    changeCompany,
    changePerson
  };
}
